/* ***** BEGIN LICENSE BLOCK *****
Version: Apache 2.0/GPL 3.0/LGPL 3.0

CCT - Computational Chemistry Tools
JMolEditor - Java Molecules Editor

Copyright 2008 Dr. Vladislav Vasilyev

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributor(s):
Dr. Vladislav Vasilyev <vvv900@gmail.com>       (original author)

Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the Apache 2.0, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the Apache 2.0, the GPL or the LGPL.

 ***** END LICENSE BLOCK *****/
package cct;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Logger;

import javax.swing.ImageIcon;

public class GlobalSettings {

  static final boolean PublicRelease = false;
  static final boolean New_Database = true;
  public static final String CUSTOM_PROPERTIES_FILE = "custom.properties";
  public static final String DIVIDER = "@";
  public static final String CWD_KEY = "cwd";
  static final String defaultFragmentDictionary = "defaultFragments.dic";
  static final String defaultSolventDictionary = "defaultSolvent.dic";
  public static final String CCT_PROPERTY_FILE = "cct/cct.properties";
  public static final String FORMATS_XML_FILE = "cct/formats.xml";
  private static Properties customProperties = null;
  private static boolean customPropsInitiated = false;
  private static URL customPropsURL = null;
  private static Properties cctProperties = null;
  private static boolean cctPropsInitiated = false;
  private static URL cctPropsURL = null;
  private static Map<String, Object> sessionProps = new HashMap<String, Object>();
  // --- Icon images
  public static final ImageIcon ICON_16x16_OPEN_FILE = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/openFile.png"));
  public static final ImageIcon ICON_16x16_GAUSSIAN = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/gaussian-16x16.png"));
  public static final ImageIcon ICON_16x16_MOPAC = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/mopac-16x16.png"));
  public static final ImageIcon ICON_16x16_GAMESS = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/banned-by-gaussian-16x16.png"));
  public static final ImageIcon ICON_16x16_ADF = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/adf-16x16.png"));
  public static final ImageIcon ICON_16x16_SIESTA = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/siesta-16x16.png"));
  public static final ImageIcon ICON_16x16_MDL = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/MDL-16x16.png"));
  public static final ImageIcon ICON_16x16_GROMACS = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/gromacs-16x16.png"));
  public static final ImageIcon ICON_16x16_VASP = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/vasp-16x16.png"));
  public static final ImageIcon ICON_16x16_QCHEM = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/qchem-16x16.png"));
  public static final ImageIcon ICON_16x16_GULP = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/gulp-16x16.png"));
  public static final ImageIcon ICON_16x16_MOLECULE = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/mol-3.png"));
  public static final ImageIcon ICON_16x16_LINE_CHART = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/line-chart.png"));
  public static final ImageIcon ICON_16x16_ADD_COMPONENT = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/component_add.png"));
  public static final ImageIcon ICON_16x16_POVRAY = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/povray-file.png"));
  public static final ImageIcon ICON_16x16_REMOTE_DOWNLOAD = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/307-web_download.png"));
  public static final ImageIcon ICON_16x16_IMAGE_FILE = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/image-file.png"));
  public static final ImageIcon ICON_16x16_GEAR = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/gear.png"));
  public static final ImageIcon ICON_16x16_SERVER_INTO = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/server_into.png"));
  public static final ImageIcon ICON_16x16_DOCUMENT_WARNING = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/document_warning.png"));
  public static final ImageIcon ICON_16x16_ERROR = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/error.png"));
  public static final ImageIcon ICON_16x16_DOCUMENT_EDIT = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/document_edit.png"));
  public static final ImageIcon ICON_16x16_DOCUMENT_INTO = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/document_into.png"));
  public static final ImageIcon ICON_16x16_DOCUMENT_OUT = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/document_out.png"));
  public static final ImageIcon ICON_16x16_ADD_ATOM = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/add-atom.gif"));
  public static final ImageIcon ICON_16x16_ADD_FRAGMENT = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/add-fragment.png"));
  public static final ImageIcon ICON_16x16_WRENCH = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/wrench.png"));
  public static final ImageIcon ICON_16x16_ATOM = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/atom.png"));
  public static final ImageIcon ICON_16x16_BOND = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/bond-16x16.gif"));
  public static final ImageIcon ICON_16x16_ANGLE = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/angle-16x16.gif"));
  public static final ImageIcon ICON_16x16_HUMMER = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/hummer-16x16.gif"));
  public static final ImageIcon ICON_16x16_CUT = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/cut-16x16.gif"));
  public static final ImageIcon ICON_16x16_CENTROID = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/centroid.png"));
  public static final ImageIcon ICON_16x16_ADD_ENTRY = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/addEntry.png"));
  public static final ImageIcon ICON_16x16_REMOVE_ENTRY = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/removeEntry.png"));
  public static final ImageIcon ICON_16x16_DIHEDRAL = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/dihedral.png"));
  public static final ImageIcon ICON_16x16_JAVA_FILE = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/java-file.png"));
  public static final ImageIcon ICON_16x16_SERVER_CLIENT = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/server_client.png"));
  public static final ImageIcon ICON_16x16_DOCUMENT_GEAR = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/document_gear.png"));
  public static final ImageIcon ICON_16x16_DOCUMENT_INFO = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/document_info.png"));
  public static final ImageIcon ICON_16x16_MEMORY_MONITOR = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/memory-monitor-16x16.png"));
  public static final ImageIcon ICON_16x16_RECYCLE = new ImageIcon(cct.resources.Resources.class.getResource("images/icons16x16/recycle.png"));

  public static final ImageIcon ICON_32x32_SERVER_FROM_CLIENT = new ImageIcon(cct.resources.Resources.class.getResource("images/icons32x32/server_from_client.png"));
  //
  static final Logger logger = Logger.getLogger(GlobalSettings.class.getCanonicalName());
  static private GlobalSettings mock = new GlobalSettings();

  private GlobalSettings() {
    getProperties();
    getCustomProperties();
  }

  static public boolean isPublicRelease() {
    return PublicRelease;
  }

  static public boolean isNewDatabase() {
    return New_Database;
  }

  static public String getDefaultFragmentDictionary() {
    return defaultFragmentDictionary;
  }

  static public String getDefaultSolventDictionary() {
    return defaultSolventDictionary;
  }

  public static Properties getCustomProperties(String propertiesFile) {

    String file = propertiesFile;
    if (file == null || file.trim().length() < 1) {
      file = CUSTOM_PROPERTIES_FILE;
    }
    customProperties = null;
    customPropsURL = null;
    try {
      customPropsURL = GlobalSettings.class.getClassLoader().getResource(cct.GlobalSettings.CUSTOM_PROPERTIES_FILE);
      customProperties = new Properties();
      customProperties.load(customPropsURL.openStream());
      customPropsInitiated = true;
      logger.info("Loaded custom properties file " + file);
    } catch (Exception ex) {
      logger.warning("Cannot open custom properties file " + file + ": " + ex.getMessage());
    }
    return customProperties;
  }

  public static Properties getCustomProperties() {
    if (customPropsInitiated) {
      return customProperties;
    }

    customProperties = getCustomProperties(CUSTOM_PROPERTIES_FILE);
    customPropsInitiated = true;
    return customProperties;
  }

  public static String getCustomPropertiesURL() {
    if (customPropsURL == null) {
      return "";
    }
    return customPropsURL.getPath();
  }

  public static Properties getProperties() {
    if (cctPropsInitiated) {
      return cctProperties;
    }

    cctProperties = getProperties(CCT_PROPERTY_FILE);
    cctPropsInitiated = true;
    return cctProperties;
  }

  public static Properties getProperties(String propertiesFile) {

    String file = propertiesFile;
    if (file == null || file.trim().length() < 1) {
      file = CCT_PROPERTY_FILE;
    }
    cctProperties = null;
    cctPropsURL = null;
    try {
      cctPropsURL = GlobalSettings.class.getClassLoader().getResource(cct.GlobalSettings.CCT_PROPERTY_FILE);
      cctProperties = new Properties();
      cctProperties.load(cctPropsURL.openStream());
      cctPropsInitiated = true;
      logger.info("Loaded cct properties file " + file);
    } catch (Exception ex) {
      logger.warning("Cannot open cct properties file " + file + ": " + ex.getMessage());
    }
    return cctProperties;
  }

  public static String getPropertiesURL() {
    if (cctPropsURL == null) {
      return "";
    }
    return cctPropsURL.getPath();
  }

  public static void setSessionProperty(String key, Object value) {
    sessionProps.put(key, value);
  }

  public static Object getSessionProperty(String key) {
    return sessionProps.get(key);
  }

  public static File getCurrentWorkingDirectory() {
    Object obj = sessionProps.get(CWD_KEY);
    if (obj == null) {
      return null;
    }
    if (obj instanceof File) {
      return (File) obj;
    }
    logger.warning("Current Working Directory should be a File, got: " + obj.getClass().getCanonicalName());
    return null;
  }

  public static void setCurrentWorkingDirectory(File pwd) {
    sessionProps.put(CWD_KEY, pwd);
  }
}
